<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.: project,
  //                    "module": module,
  //                    "func": func,
  //                    "detail": detail,
  //                    "people": people,
  //                    "time": time
 */

class ScheduleService {

    public function addschedule($arr = array()) {
        if (isset($arr["project"]) && isset($arr["sid"]) && isset($arr["adder"]) && isset($arr["module"]) && isset($arr["func"]) && isset($arr["detail"]) && isset($arr["people"]) && isset($arr["dateline"]) && isset($arr["addtime"])) {
//            if(strlen($arr["sid"])>10||strlen($arr["project"])>16||strlen($arr["module"])>16||strlen($arr["func"])>16||strlen($arr["people"])>10||strlen($arr["adder"])>12){
//                 return JSONHelper::ERROR_ILLEGAL_REQUEST;  
//            }

            $schedule = new Schedule();
            $schedule->sid = $arr["sid"];
            $schedule->project = $arr["project"];
            $schedule->module = $arr["module"];
            $schedule->function = $arr["func"];
            $schedule->detail = $arr["detail"];
            $schedule->people = $arr["people"];
            $schedule->adder = $arr["adder"];
            $schedule->dateline = $arr["dateline"];
            $schedule->addtime = $arr["addtime"];
            $schedule->save();
            return JSONHelper::SUCCESS;
        } else {
            return JSONHelper::ERROR_ILLEGAL_REQUEST;
        }
    }

    public function updateschedule($arr = array()) {
        if (isset($arr["project"]) && isset($arr["sid"]) && isset($arr["adder"]) && isset($arr["module"]) && isset($arr["func"]) && isset($arr["detail"]) && isset($arr["people"]) && isset($arr["dateline"]) && isset($arr["addtime"])) {
//            if(strlen($arr["sid"])>10||strlen($arr["project"])>16||strlen($arr["module"])>16||strlen($arr["func"])>16||strlen($arr["people"])>10||strlen($arr["adder"])>12){
//                 return JSONHelper::ERROR_ILLEGAL_REQUEST;  
//            }

            $schedule = Schedule::model()->find("sid=:sid", array("sid" => $arr["sid"]));
            $schedule->sid = $arr["sid"];
            $schedule->project = $arr["project"];
            $schedule->module = $arr["module"];
            $schedule->function = $arr["func"];
            $schedule->detail = $arr["detail"];
            $schedule->people = $arr["people"];
            $schedule->adder = $arr["adder"];
            $schedule->dateline = $arr["dateline"];
            $schedule->addtime = $arr["addtime"];
            $schedule->save();

            $list = array();

            $list["sid"] = $schedule->sid;
            $list["project"] = $schedule->project;
            $list["module"] = $schedule->module;
            $list["func"] = $schedule->function;
            $list["detail"] = $schedule->detail;
            $list["people"] = $schedule->people;
            $list["adder"] = $schedule->adder;
            $list["dateline"] = $schedule->dateline;
            $list["addtime"] = $schedule->addtime;
            return $list;
        }
    }

    /**
     * 获得数据表中数据个数
     * @return int
     */
    private function getTotal() {
        return $Schedule::model()->count();
    }

    /**
     * 获得项目列表
     */
    function getProject() {
        //$proList = Schedule::model()->find(array('select' => 'DISTINCT project', 'condition' => 'project!="" order by project'));
        $proList = Schedule::model()->findAll(array('select' => 'DISTINCT project', 'condition' => 'project!="" order by project'));//使用findall
        
        $pronum = count($proList);
        $list = array();
        $i = 0;
        while ($i < $pronum) {
            $list[$i]["project"] = $proList[$i]->project;
            $i++;
        }
        return $list;
    }
   /**
     * 获得日期列表
     */
    function getDateline() {
        //$proList = Schedule::model()->find(array('select' => 'DISTINCT project', 'condition' => 'project!="" order by project'));
        $datelineList = Schedule::model()->findAll(array('select' => 'DISTINCT dateline', 'condition' => 'dateline!="" order by dateline desc'));//使用findall
        
        $num = count($datelineList);
        $list = array();
        $i = 0;
        while ($i < $num) {
            $list[$i]["dateline"] = $datelineList[$i]->dateline;
            $i++;
        }
        return $list;
    }
    /**
     * 获得分页的显示代码的html
     */
    function getFpage($num, $o = TRUE) {
        $count = $this->getTotal();
        $page = new PageService($count, 20, $num, $o);
    }
    /**
     * 获得分页内容
     */
    function getPage($num) {
        $page=$num*15;
         $resultList = Schedule::model()->findAll("dateline!='' order by dateline desc limit $page,15");
        $list = array();
        $i = 0;
        while ($i < count($resultList)) {
            $list[$i]["sid"] = $resultList[$i]->sid;
            $list[$i]["dateline"] = $resultList[$i]->dateline;
            $list[$i]["project"] = $resultList[$i]->project;
            $list[$i]["module"] = $resultList[$i]->module;
            $list[$i]["func"] = $resultList[$i]->function;
            $list[$i]["detail"] = $resultList[$i]->detail;
            $list[$i]["people"] = $resultList[$i]->people;
            $list[$i]["adder"] = $resultList[$i]->adder;
            $list[$i]["addtime"] = $resultList[$i]->addtime;

//           = ;
//            $row->addtime = $list["addtime"];
            // $list[i]=$row;
            $i++;
        }
        return $list;
    }
    /**
     * 获得指定页的信息
     * @return type
     */
    function getOnePageSchedule() {
        $resultList = Schedule::model()->findAll("dateline!='' order by dateline desc");
        $list = array();
        $i = 0;
        while ($i < count($resultList)) {
            $list[$i]["sid"] = $resultList[$i]->sid;
            $list[$i]["dateline"] = $resultList[$i]->dateline;
            $list[$i]["project"] = $resultList[$i]->project;
            $list[$i]["module"] = $resultList[$i]->module;
            $list[$i]["func"] = $resultList[$i]->function;
            $list[$i]["detail"] = $resultList[$i]->detail;
            $list[$i]["people"] = $resultList[$i]->people;
            $list[$i]["adder"] = $resultList[$i]->adder;
            $list[$i]["addtime"] = $resultList[$i]->addtime;

//           = ;
//            $row->addtime = $list["addtime"];
            // $list[i]=$row;
            $i++;
        }
        return $list;
    }

    function getAllScheduleDetials() {
        $resultList = Schedule::model()->findAll("dateline!='' order by dateline desc");
        $list = array();
        $i = 0;
        while ($i < count($resultList)) {
            $list[$i]["sid"] = $resultList[$i]->sid;
            $list[$i]["dateline"] = $resultList[$i]->dateline;
            $list[$i]["project"] = $resultList[$i]->project;
            $list[$i]["module"] = $resultList[$i]->module;
            $list[$i]["func"] = $resultList[$i]->function;
            $list[$i]["detail"] = $resultList[$i]->detail;
            $list[$i]["people"] = $resultList[$i]->people;
            $list[$i]["adder"] = $resultList[$i]->adder;
            $list[$i]["addtime"] = $resultList[$i]->addtime;

//           = ;
//            $row->addtime = $list["addtime"];
            // $list[i]=$row;
            $i++;
        }
        return $list;
    }

    /**
     * 获得用户排期的内容，按照日期排序后汇集
     * @return type
     */
    function scheduleList() {
        //  $scheduleList=new Schedule();"order by dateline",""
        $resultList = Schedule::model()->findAll("dateline!='' order by dateline desc");
        //   $resultList=
        $list = array();
        $i = 0;
        while ($i < count($resultList)) {
            $list[$i]["sid"] = $resultList[$i]->sid;
            $list[$i]["project"] = $resultList[$i]->project;
            $list[$i]["module"] = $resultList[$i]->module;
            $list[$i]["func"] = $resultList[$i]->function;
            $list[$i]["detail"] = $resultList[$i]->detail;
            $list[$i]["people"] = $resultList[$i]->people;
            $list[$i]["adder"] = $resultList[$i]->adder;
            $list[$i]["dateline"] = $resultList[$i]->dateline;
//           = ;
//            $row->addtime = $list["addtime"];
            // $list[i]=$row;
            $i++;
        }
        //   return $list;
        $resultFormat = $this->scheduleListFormat($list);
        return $resultFormat;
    }
 /**
     * 获得用户排期的内容，按照日期排序并按项目排序后后汇集
     * @return type
     */
    function newscheduleList() {
        //  $scheduleList=new Schedule();"order by dateline",""
        $resultList = Schedule::model()->findAll("dateline!='' order by dateline desc,project");
        //   $resultList=
        $list = array();
        $i = 0;
        while ($i < count($resultList)) {
            $list[$i]["sid"] = $resultList[$i]->sid;
            $list[$i]["project"] = $resultList[$i]->project;
            $list[$i]["module"] = $resultList[$i]->module;
            $list[$i]["func"] = $resultList[$i]->function;
            $list[$i]["detail"] = $resultList[$i]->detail;
            $list[$i]["people"] = $resultList[$i]->people;
            $list[$i]["adder"] = $resultList[$i]->adder;
            $list[$i]["dateline"] = $resultList[$i]->dateline;
//           = ;
//            $row->addtime = $list["addtime"];
            // $list[i]=$row;
            $i++;
        }
        //   return $list;
        $resultFormat = $this->newscheduleListFormat($list);
        return $resultFormat;
    }
    /**
     * 将排期内容从数组中组装格式化，便于前端显示
     * @param type $arr
     * @return type
     */
    private function scheduleListFormat1($arr = array()) {
        $tempdateline = $arr[0]["dateline"]; //存储当前排期日期，识别相同日期
        $result = array(); //存储格式化后的结果
        $i = 0; //遍历数组
        $j = 0; //记录result的数组下标
        // return  $arr[$i]["dateline"];
        // return   count($arr);
        $num = 1;
        while ($i < count($arr)) {//要小于不应该等于
            //记录任务数量，多余的忽略
            if ($tempdateline == $arr[$i]["dateline"]) {
                if (!isset($result[$j]["dateline"])) {
                    $result[$j]["dateline"] = $arr[$i]["dateline"];
                }
                switch ($num) {
                    case 1:
                        $result[$j]["one"] = $arr[$i]["project"];
                        break;
                    case 2:
                        $result[$j]["two"] = $arr[$i]["project"];
                        break;
                    case 3:
                        $result[$j]["three"] = $arr[$i]["project"];
                        break;
                    case 4:
                        $result[$j]["four"] = $arr[$i]["project"];
                        break;
                    case 5:
                        $result[$j]["five"] = $arr[$i]["project"];
                        break;
                    default:

                        break;
                }
                $num++;

                // $result[$j]["dateline"]=$tempdateline;
            } else {
                $tempdateline = $arr[$i]["dateline"];
                $result[$j]["dateline"] = $arr[$i]["dateline"];
                $result[$j]["one"] = $arr[$i]["project"];
                $num = 2; //因为one已经有了，所以num置2
                $j++;
            }
            $i++;
        }
        return $result;
    }

    private function scheduleListFormat($arr = array()) {
        $tempdateline = $arr[0]["dateline"]; //存储当前排期日期，识别相同日期
        $result = array(); //存储格式化后的结果
        $i = 0; //遍历数组
        $j = 0; //记录result的数组下标
        // return  $arr[$i]["dateline"];
        // return   count($arr);
        $num = 1;
        while ($i < count($arr)) {//要小于不应该等于
            //记录任务数量，多余的忽略
            if ($tempdateline == $arr[$i]["dateline"]) {

                if (!isset($result[$j]["dateline"])) {
                    $result[$j]["dateline"] = $arr[$i]["dateline"];
                }
                switch ($num) {
                    case 1:
                        $result[$j]["one"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                        break;
                    case 2:
                        $result[$j]["two"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                        break;
                    case 3:
                        $result[$j]["three"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                        break;
                    case 4:
                        $result[$j]["four"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                        break;
                    case 5:
                        $result[$j]["five"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                        break;
                    default:

                        break;
                }
                $num++;

                // $result[$j]["dateline"]=$tempdateline;
                // } else {
                //       $tempdateline = $arr[$i]["dateline"];
                //  $result[$j]["one"] = $arr[$i]["project"];
                //   $num = 2; //因为one已经有了，所以num置2
                //     }
            } else {
                $tempdateline = $arr[$i]["dateline"];
                $j++;
                $result[$j]["dateline"] = $arr[$i]["dateline"];
                $result[$j]["one"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                $num = 2; //因为one已经有了，所以num置2
            }

            $i++;
        }
        return $result;
    }
/**
 * 格式化，建立日期与项目二维数组arr[日期][项目]
 * @param type $arr
 * @return string
 */
    private function newscheduleListFormat($arr = array()) {
        $tempdateline = $arr[0]["dateline"]; //存储当前排期日期，识别相同日期
        $result = array(); //存储格式化后的结果
        $i = 0; //遍历数组
        $j = 0; //记录result的数组下标
    
        $num = 1;
        while ($i < count($arr)) {//要小于不应该等于
            //记录任务数量，多余的忽略
            if ($tempdateline == $arr[$i]["dateline"]) {
                if (!isset($result[$j]["dateline"])) {
                    $result[$j]["dateline"] = $arr[$i]["dateline"];
                }
                $pro=$arr[$i]["project"];
                 $result[$j][$pro] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
//                switch ($num) {
//                    case 1:
//                        $result[$j]["one"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
//                        break;
//                    case 2:
//                        $result[$j]["two"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
//                        break;
//                    case 3:
//                        $result[$j]["three"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
//                        break;
//                    case 4:
//                        $result[$j]["four"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
//                        break;
//                    case 5:
//                        $result[$j]["five"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
//                        break;
//                    default:
//
//                        break;
//                }
                $num++;

            } else {
                $tempdateline = $arr[$i]["dateline"];
                $j++;
                $result[$j]["dateline"] = $arr[$i]["dateline"];
                $pro=$arr[$i]["project"];
                 $result[$j][$pro] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                
              //  $result[$j]["one"] = "【" . $arr[$i]["project"] . "】" . "->" . $arr[$i]["module"] . "->" . $arr[$i]["func"];
                $num = 2; //因为one已经有了，所以num置2
            }

            $i++;
        }
        return $result;
    }

}
